﻿
using KingRoad.EPM.CompletionMaterialsBLL.UIRules;
using KingRoad.EPM.CompletionMaterialsDAL.BcdProfiletypeService;
using KingRoad.EPM.CompletionMaterialsDAL.BcdVolumeDirectoryService;
using KingRoad.EPM.CompletionMaterialsEntity.Model;
using KingRoad.EPM.Web.Areas.CompletionMaterials.CompletionMaterialsFramework.Common;
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Web.Script.Serialization;
using System.Web.Services;

namespace KingRoad.EPM.Web
{
    /// <summary>
    /// ComWebService 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务，请取消注释以下行。 
    // [System.Web.Script.Services.ScriptService]
    public class ComWebService : System.Web.Services.WebService
    {
        /// <summary>
        /// 资料类型树节点集合
        /// </summary>
        public class treeNodes
        {
            public treeNodes() { }
            public string id { get; set; }
            public string pId { get; set; }
            public string name { get; set; }
        }

        public class Json
        {
            public Json() { }
            public string statusCode { get; set; }
            public string message { get; set; }
        }

        /// <summary>
        /// 获取竣工资料类型树
        /// </summary>
        /// <param name="projectId">项目id</param>
        /// <param name="id">类型id,获取根节点时传空值</param>
        /// <returns></returns>
        [WebMethod]
        public void GetFileType(int projectId, string id)
        { 
            JavaScriptSerializer jss = new JavaScriptSerializer();
            try
            {
                BcdProfiletypeUIRule _bcdProfiletypeUIRule = new BcdProfiletypeUIRule(new BcdProfiletypeService());
                List<treeNodes> tList = new List<treeNodes>();
                string sql = " P_ID=" + projectId;
                if (id != "")
                    sql += " and Pft_PID='" + id + "' ";
                else
                    sql += " and Pft_PID is null ";
                List<BcdProfiletypeInfo> List = _bcdProfiletypeUIRule.GetEntityListBySql(sql);
                foreach (BcdProfiletypeInfo info in List)
                {
                    treeNodes node = new treeNodes();
                    node.id = info.Pft_ID;
                    node.pId = info.Pft_PID;
                    node.name = info.Fd_Name;
                    tList.Add(node);
                }
                Context.Response.Write(jss.Serialize(tList));
            }
            catch(Exception)
            {
                List<Json> jList = new List<Json>();
                Json json = new Json();
                json.statusCode = "300";
                json.message = "获取信息失败";
                Context.Response.Write(jss.Serialize(jList));
            }
            Context.Response.End(); 
        }

        /// <summary>
        /// 添加竣工资料文件
        /// </summary>
        /// <param name="tId">资料类型id</param>
        /// <param name="code">文件编号</param>
        /// <param name="duty">责任者</param>
        /// <param name="name">文件名</param>
        /// <param name="date">文件日期</param>
        /// <param name="page">文件页数</param>
        /// <param name="remark">文件备注</param>
        /// <param name="userId">用户Id</param>
        /// <param name="userName">用户名</param>
        /// <returns></returns>
        [WebMethod]
        public void AddFile(string tId, string code, string duty, string name, DateTime date, int page, string remark, int userId, string userName)
        {
            var isScuess = TryCatchCommand.TryExec(() =>
            {
                BcdVolumedirectoryUIRule _bcdVolumedirectoryUIRule = new BcdVolumedirectoryUIRule(new BcdVolumedirectoryService());
                BcdVolumedirectoryInfo info = new BcdVolumedirectoryInfo();
                info.Vd_ID = Guid.NewGuid().ToString().ToUpper();
                info.Pft_ID = tId;
                info.Vd_Code = code;
                info.Vd_Duty = duty;
                info.Vd_Name = name;
                info.Vd_Date = date;
                info.Vd_PageNumb = page;
                info.Vd_Remark = remark;
                info.Vd_OperateUserID = userId;
                info.Vd_OperateUserName = userName;
                info.Vd_OperateTime = DateTime.Now;

                _bcdVolumedirectoryUIRule.InsertEntity(info);
            });
            List<Json> jList = new List<Json>();
            Json json = new Json();
            dynamic jsonReturn = new ExpandoObject();
            if (isScuess)
            {
                json.statusCode = "200";
                json.message = "添加成功";
            }
            else
            {
                json.statusCode = "300";
                json.message = "添加失败";
            }
            jList.Add(json);
            JavaScriptSerializer jss = new JavaScriptSerializer();
            Context.Response.Write(jss.Serialize(jList));
            Context.Response.End(); 
        }
    }
}
